Skip to main content

Conceptual Asides

Execution Context (Global)​

Main Components:

  • Global Object
    • Global = Not Inside a Function
  • this - whoever is running the application.
    • in browser it is a window object
    • in other execution context, this would be different
  • Outer Environment
  • Your Code

Hoisting​

b();
console.log(a);

var a = 'Hello World';

function b(){
console.log('Called b!')
}

OUTPUTS:

undefined
Called b!
  • function b - works because on the initial load of JS, they pass through the code and function b is allocated with a memory space.
  • a is allocated a space as well, but currently is undefined, only when the code actually runs is when Hello World is assigned to a
  • This is called hoisting, JS engine 'hoists' the functions and variable declarations to the top of the code.

ES6​

  • ECMAScript - Next version of JS

Synchronous​

Typing​

  • No such thing as Static Typing in JS by default, everything is Dynamic Typed.
    • Everything is figured out on the fly

Coercion​

  • Since vars are dynamically typed, when you try to add different types, JS converts to a specific types

Strict Equality​

=== - do not do any coercion/typing when comparing values

Object

can contains:

- Primitive Property
- another Object Property
- Function Method

Object Property​

Dynamic Property access:​

var person = new Object();
person["firstname"] = "Tony";

can be access:

var property = "firstname";
console.log(person[property]);

Dot​

console.log(person.firstname);
  • We can nest
    person.address = new Object();
person.address.street = "111 main"

Literals​

  • instead of new Object
    var person = {};
  • They are the same under the hood.
  • so we can shorthand it declare:
    var person = { firstname: 'Tony'};

SAME AS:

    var person = new Object();
person.firstname = 'tony'

SAME AS:

    var person = new Object();
person["firstname"]= "tony";

JSON vs Object Literals​

  • JSON is a more strict rules
  • Object literals are not qouted
  • they are not the same

**Convert Object Literal to JSON

var objectLiteral = {
firstName: 'Mary'
}

JSON.stringify(objectLiteral)

JSON String to Object Literal

var object = JSON.parse ('{ "firstname": "Mary"}')

Higher Order Functions​

  • Functions are Objects
  • You can add:
    • Primitive
    • Object
    • Function
    • Name (optional)
    • Code (Invocable)
function greet(){
console.log('hi');
}

greet.language = 'English';

Function statement vs expression​

Statement:

    funcion greet(){
console.log('hi');
}

Expression (non-hoisted

  • Anonymous function assigned to variable
var anon = function(){
console.log('hi');
}
anon();

Pass it around:

function log (a){
a();
}

log(()=>{
console.log('helo');
})

This​

  • Dynamically changes based on where it is attached:

Method THIS​

PITFALL by default this refers to the global execution context:

    var a = function(){
this.newVariable ='global'
}

a();
console.log(newVariable);

Object THIS​

var c = {
name: 'c object',
log: ()=>{
console.log(this)
}
}
c.log(); // 'c object'

Mutate (object)​

var person = {
name: "mel",
changeName: function() {
this.name = "april"
},
getName: function() {
console.log(this.name);
}
}

person.changeName();
console.log(person.getName());

Self​

PITFALL : nested this calls goes back to refering to the global reference of this:

var person = {
name: "mel",
changeName: function() {
this.name = "april"
var setname = function(newname){
this.name = newname; //GLOBAL this
}
setname("bug")
},
getName: function() {
console.log(this.name);
}
}

To work around this:

changeName: function() {
var self = this;
self.name = "april"
var setname = function(newname){
self.name = newname; //GLOBAL this
}
setname("bug")
}